/*
	Copyright 2009 Omer Saatcioglu
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

	$Author$
	$Revision$
	$LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import android.util.Log;

public class CtrlGuesstheNumber {

	static private CtrlGuesstheNumber ctrlGuesstheNumber;
	private int theSecretNumber;
	private int nDigits;

	static public synchronized CtrlGuesstheNumber getInstance() {
		if (ctrlGuesstheNumber == null) {
			ctrlGuesstheNumber = new CtrlGuesstheNumber();
		}
		return ctrlGuesstheNumber;
	}

	private CtrlGuesstheNumber() {
		// The class is singleton.
	}

	public void setDigitNumber(int ndigits) {
		nDigits = ndigits;
	}

	public int[] ProcesstheNumber(int theNumber) {
		int[] arrtheNumber = gettheDigits(theNumber);
		int[] arrtheSecretNumber = gettheDigits(theSecretNumber);
		int[] theResult = { 0, 0 };

		for (int i = 0; i < nDigits; i++) {
			if (arrtheNumber[i] == arrtheSecretNumber[i]) {
				theResult[0]++;
			}
		}
		for (int i = 0; i < nDigits; i++) {
			for (int j = 0; j < nDigits; j++) {
				if (i != j && arrtheNumber[i] == arrtheSecretNumber[j]) {
					theResult[1]++;
				}
			}
		}
		return theResult;
	}

	public int gettheSecretNumber() {
		return theSecretNumber;
	}

	public void settheSecretNumber() {
		theSecretNumber = graptheSecretNumber();
		Log.d("CtrlGuesstheNumber::settheSecretNumber", Integer.toString(theSecretNumber));
	}

	public void settheSecretNumber(int intSecretNumber) {
		theSecretNumber = intSecretNumber;
		Log.d("CtrlGuesstheNumber::settheSecretNumber", Integer.toString(theSecretNumber));
	}

	private int graptheSecretNumber() {
		int base = (int) Math.pow(10.0, (double) (nDigits - 1));
		int ceil = (int) Math.pow(10.0, (double) (nDigits));

		int theNumber = (int) (ceil * Math.random()) + base;
		if (theNumber < base || theNumber >= ceil) {
			return graptheSecretNumber();
		}
		int[] arrtheNumber = gettheDigits(theNumber);
		for (int i = 0; i < nDigits; i++) {
			for (int j = i; j < nDigits; j++) {
				if (i != j && arrtheNumber[i] == arrtheNumber[j]) {
					return graptheSecretNumber();
				}
			}
		}
		return theNumber;
	}

	private int[] gettheDigits(int theNumber) {
		int length = Integer.toString(theNumber).length();
		if (length == 0) {
			length = 1;
		}
		int[] arrtheNumber = new int[length];
		for (int i = 0; i < length; i++) {
			int theFirstNumber = theNumber - theNumber
					/ (int) Math.pow(10.0, (double) (i + 1))
					* (int) Math.pow(10.0, (double) (i + 1));
			int theSecondNumber = theNumber - theNumber
					/ (int) Math.pow(10.0, (double) (i))
					* (int) Math.pow(10.0, (double) (i));
			arrtheNumber[i] = (theFirstNumber - theSecondNumber)
					/ (int) Math.pow(10.0, (double) i);
		}
		return arrtheNumber;
	}

}
